<?php

namespace web\api\controller;

/**
 * Description of News
 * 约pa
 */
class AboutPa extends UserBase {

    /**
     * 发布约pa
     */
    public function releaseYuePa(){
        try {
            $add_data['title'] = $this->getParamVal('title');
            $add_data['type'] = $this->getParamVal('type');
            $add_data['province_id'] = $this->getParamVal('province_id');
            $add_data['province'] = $this->getParamVal('province');
            $add_data['city_id'] = $this->getParamVal('city_id');
            $add_data['city'] = $this->getParamVal('city');
            $add_data['area_id'] = $this->_post('area_id');
            $add_data['area'] = $this->_post('area');
            $add_data['mm_number'] = $this->getParamVal('mm_number');
            $add_data['mm_age'] = $this->getParamVal('mm_age');
            $add_data['service_project'] = $this->getParamVal('service_project');
            $add_data['service_price'] = $this->getParamVal('service_price');
            $add_data['contact_phone'] = $this->getParamVal('contact_phone');
            $add_data['describe'] = $this->getParamVal('describe');
            $add_data['describe'] = htmlspecialchars_decode($add_data['describe']);
            $add_data['address'] = $this->getParamVal('address');
            $add_data['video_url'] = $this->_post('video_url');
            $add_data['mm_images'] = $this->getParamVal('mm_images');
            $add_data['create_time'] = NOW_DATETIME;
            $add_data['user_id'] = $this->user_id;

            $re = (new \addons\about\model\About())->add($add_data);
            if(!$re){
                return $this->failJSON('发布失败，请稍后再试');
            }
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 我发布约pa列表
     */
    public function myPaList(){
        try {
            $aboutM = new \addons\about\model\About();
            $user_id = $this->user_id;
            $filter = ' user_id = '.$user_id;
            $fields = ' * ';
            $order = 'id desc';
            $list = $aboutM->getDataList($this->getPageIndex(), $this->getPageSize(), $filter, $fields, $order);
            if($list){
                foreach ($list as &$value){
                    $value['mm_images'] = explode(",", $value['mm_images']);

                    $value['describe'] = htmlspecialchars_decode($value['describe']);
                }
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 约PA广场列表
     */
    public function squareList(){
        try {
            $aboutM = new \addons\about\model\About();
            $aboutLikeM = new \addons\about\model\AboutLike();
            $type = $this->getParamVal('type');
            $address_type = $this->_post('address_type')??'';
            $address_id = $this->_post('address_id')??'';
            $filter = ' a.status = 1 and a.type = '.$type;

            if($address_type == 1){
                $filter .= ' and a.province_id = '.$address_id ;
            }else if($address_type == 2){
                $filter .= ' and a.city_id = '.$address_id ;
            }else if($address_type == 3){
                $filter .= ' and a.area_id = '.$address_id ;
            }

            $fields = ' a.*,b.username,b.phone ';
            $order = ' a.like_number desc ';
            $list = $aboutM->getAboutList($this->getPageIndex(), $this->getPageSize(), $filter, $fields, $order);
            if($list){
                foreach ($list as &$value){
                    $value['mm_images'] = explode(",", $value['mm_images']);
                    $value['describe'] = htmlspecialchars_decode($value['describe']);
                    $like_data = $aboutLikeM->where(['user_id' => $this->user_id, 'about_id' => $value['id']])->find();
                    $value['is_like'] = 0;
                    if($like_data){
                        $value['is_like']= 1;
                    }
                }
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 约PA详情
     */
    public function squareListDetails(){
        try {
            $id = $this->getParamVal('id');
            $aboutM = new \addons\about\model\About();
            $about_data = $aboutM->getDetail($id);
            $about_data['mm_images'] = explode(",", $about_data['mm_images']);

            $about_data['describe'] = htmlspecialchars_decode($about_data['describe']);
            if(empty($about_data)){
                $this->failJSON('该用户已下架');
            }

            $like_data = (new \addons\about\model\AboutLike())->where(['user_id' => $this->user_id, 'about_id' => $id])->find();
            $about_data['is_like']= 0;
            if($like_data){
                $about_data['is_like']= 1;
            }
            $accountM = new \addons\member\model\MemberAccountModel();
            $user_data = $accountM->getDetail($about_data['user_id']);

            $about_data['username'] = $user_data['username'];
            $about_data['phone'] = $user_data['phone'];
            $about_data['head_img'] = $user_data['head_img'];
            return $this->successJSON($about_data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * PA下单
     */
    public function addPlaceOrder(){
        try {
            $id = $this->getParamVal('id');
            $aboutM = new \addons\about\model\About();
            $about_data = $aboutM->getDetail($id);
            if(empty($about_data)){
                return $this->failJSON('该用户已下架');
            }

            $user_id = $this->user_id;
            $balance_type = self::USDT_TYPE;

            $accountM = new \addons\member\model\MemberAccountModel();
            $user_data = $accountM->getDetail($user_id);
            if($user_data['level'] < 1){
                return $this->failJSON('需要VIP才能购买');
            }

            $balanceM = new \addons\member\model\Balance();
            $user_balance = $balanceM->getBalanceAmountByType($user_id, $balance_type);
            if($user_balance < $about_data['service_price']){
                return $this->failJSON('余额不足');
            }

            //扣款
            $balanceM->startTrans();
            $recordM = new \addons\member\model\TradingRecord();
            $re_balance = $balanceM->updateBalance($user_id, $balance_type, $about_data['service_price'], false);
            if(!$re_balance){
                $balanceM->rollback();
                return $this->failJSON('用户扣款失败');
            }
            $re_record = $recordM->addRecord($user_id, $about_data['service_price'], $re_balance['before_amount'], $re_balance['amount'], $balance_type, 1000, 0, 0, '约pa下单');
            if(!$re_record){
                $balanceM->rollback();
                return $this->failJSON('用户扣款记录失败');
            }

            //生成订单
            $add_data['user_id'] = $user_id;
            $add_data['about_id'] = $about_data['id'];
            $add_data['about_user_id'] = $about_data['user_id'];
            $add_data['title'] = $about_data['title'];
            $add_data['province_id'] = $about_data['province_id'];
            $add_data['province'] = $about_data['province'];
            $add_data['city_id'] = $about_data['city_id'];
            $add_data['city'] = $about_data['city'];
            $add_data['area_id'] = $about_data['area_id'];
            $add_data['area'] = $about_data['area'];
            $add_data['service_project'] = $about_data['service_project'];
            $add_data['service_price'] = $about_data['service_price'];
            $add_data['contact_phone'] = $about_data['contact_phone'];
            $add_data['describe'] = $about_data['describe'];
            $add_data['address'] = $about_data['address'];
            $add_data['create_time'] = NOW_DATETIME;

            $aboutOrderM = new \addons\about\model\AboutOrder();
            $re = $aboutOrderM->add($add_data);
            if(!$re){
                $balanceM->rollback();
                return $this->failJSON('创建订单失败');
            }

            (new \addons\config\model\SystemMessage())->message_add($user_id, '约pa订单','您有一份约pa订单，请及时前往查询',2);

            $balanceM->commit();
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 我购买的约pa订单列表
     */
    public function myPlaceList(){
        try {
            $aboutOrderM = new \addons\about\model\AboutOrder();
            $user_id = $this->user_id;
            $filter = ' a.user_id = '.$user_id;
            $fields = ' a.*,t.username,ab.mm_images';
            $order = 'a.id desc';
            $list = $aboutOrderM->getAboutOrderList($this->getPageIndex(), $this->getPageSize(), $filter, $fields, $order);
            if($list){
                $aboutLikeM = new \addons\about\model\AboutLike();
                foreach ($list as &$value){
                    $value['mm_images'] = explode(",", $value['mm_images']);

                    $value['describe'] = htmlspecialchars_decode($value['describe']);
                    $like_data = $aboutLikeM->where(['user_id' => $this->user_id, 'about_id' => $value['id']])->find();
                    $value['is_like']= 0;
                    if($like_data){
                        $value['is_like']= 1;
                    }
                }
            }

            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 我收到的约pa订单列表
     */
    public function aboutPlaceList(){
        try {
            $aboutOrderM = new \addons\about\model\AboutOrder();
            $user_id = $this->user_id;
            $filter = ' a.about_user_id = '.$user_id;
            $fields = ' a.*,b.username,ab.mm_images';
            $order = 'a.id desc';
            $list = $aboutOrderM->getAboutOrderList($this->getPageIndex(), $this->getPageSize(), $filter, $fields, $order);
            if($list){
                foreach ($list as &$value){
                    $value['describe'] = htmlspecialchars_decode($value['describe']);
                    $value['mm_images'] = explode(",", $value['mm_images']);
                }
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 收到的订单 MM方发货
     */
    public function deliveryOrder(){
        try {
            $aboutOrderM = new \addons\about\model\AboutOrder();
            $id = $this->getParamVal('id');
            $order_data = $aboutOrderM->where(['id' => $id, 'about_user_id' => $this->user_id, 'status' => 0])->find();
            if(empty($order_data)){
                return $this->failJSON('请勿重复提交');
            }

            $update_data['update_time'] = NOW_DATETIME;
            $update_data['status'] = 2;

            $re = $aboutOrderM->where(['id' => $id])->update($update_data);
            if(!$re){
                return $this->failJSON('提交失败，请稍后再试');
            }
            (new \addons\config\model\SystemMessage())->message_add($order_data['user_id'], '约pa订单','您有一份约pa发货订单，请及时前往查询',2);
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 完成订单
     */
    public function completePlaceOrder(){
        try {
            $aboutOrderM = new \addons\about\model\AboutOrder();
            $id = $this->getParamVal('id');
            $order_data = $aboutOrderM->where(['id' => $id, 'user_id' => $this->user_id, 'status' => 2])->find();
            if(empty($order_data)){
                return $this->failJSON('请勿重复提交');
            }
            $balanceM = new \addons\member\model\Balance();
            //扣款
            $balanceM->startTrans();
            $balance_type = self::USDT_TYPE;
            $recordM = new \addons\member\model\TradingRecord();
            $re_balance = $balanceM->updateBalance($order_data['about_user_id'], $balance_type, $order_data['service_price'], true);
            if(!$re_balance){
                $balanceM->rollback();
                return $this->failJSON('用户增额失败');
            }
            $re_record = $recordM->addRecord($order_data['about_user_id'], $order_data['service_price'], $re_balance['before_amount'], $re_balance['amount'], $balance_type, 1001, 1, $this->user_id, '约pa完成');
            if(!$re_record){
                $balanceM->rollback();
                return $this->failJSON('用户增额记录失败');
            }

            $update_data['update_time'] = NOW_DATETIME;
            $update_data['status'] = 1;

            $re = $aboutOrderM->where(['id' => $id])->update($update_data);
            if(!$re){
                $balanceM->rollback();
                return $this->failJSON('提交失败，请稍后再试');
            }

            (new \addons\config\model\SystemMessage())->message_add($order_data['about_user_id'], '约pa订单','您有一份约pa完成订单，请及时前往查询',2);

            $balanceM->commit();
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 约pa喜欢
     */
    public function aboutLike(){
        try {
            $id = $this->getParamVal('id');
            $type = $this->getParamVal('type');
            $aboutM = new \addons\about\model\About();
            $aboutLikeM = new \addons\about\model\AboutLike();
            if($type == 1){
                $about_like_data = $aboutLikeM->where(['user_id' => $this->user_id, 'about_id' => $id])->find();
                if($about_like_data){
                    return $this->failJSON('请勿重复点击');
                }
                $re = $aboutLikeM->add(['user_id' => $this->user_id, 'about_id' => $id, 'create_time' => NOW_DATETIME]);
                if(!$re){
                    return $this->failJSON('请勿重复点击');
                }
                $aboutM->where(['id' => $id])->setInc('like_number', 1);
            }else{
                $about_like_data = $aboutLikeM->where(['user_id' => $this->user_id, 'about_id' => $id])->find();
                if(empty($about_like_data)){
                    return $this->failJSON('请勿重复点击');
                }
                $re = $aboutLikeM->where(['user_id' => $this->user_id, 'about_id' => $id])->delete();
                if(!$re){
                    return $this->failJSON('请勿重复点击');
                }
            }
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 约pa 喜欢列表
     */
    public function likeList(){
        try {
            $aboutLikeM = new \addons\about\model\AboutLike();
            $filter = ' a.user_id = '.$this->user_id;
            $fields = ' a.id,a.user_id,a.about_id,b.create_time,b.title,b.service_price,b.service_project,b.province,b.province_id,b.city_id,b.city,b.area,b.area_id,b.like_number,b.video_url,b.mm_images';
            $order = 'a.id desc';
            $list = $aboutLikeM->getLikeList($this->getPageIndex(), $this->getPageSize(), $filter, $fields, $order);
            if($list){
                $aboutLikeM = new \addons\about\model\AboutLike();
                foreach ($list as &$value){
                    $like_data = $aboutLikeM->where(['user_id' => $this->user_id, 'about_id' => $value['id']])->find();
                    $value['is_like']= 0;
                    if($like_data){
                        $value['is_like']= 1;
                    }
                    $value['mm_images'] = explode(",", $value['mm_images']);
                }
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }



}
